<template>
  <Main>
    <PageHeader class="header-wrapper" :text="category">
      <!-- <van-icon class-prefix="custom-iconfont" @click="handleClickIcon" name="folder1" /> -->
      <!-- <van-icon name="comment-o" @click="handleClickIcon" /> -->
    </PageHeader>
    <div class="category-list variables-wrapper">
      <div class="booklist-data-wrapper">
        <div class="book-item" v-for="(book, $index) in bookListData" :key="$index" @click="gotoBookDetail(book)">
          <div class="book-cover-wrapper">
            <BookCover :src="book.avatar" />
          </div>
          <p class="book-name">{{ book.name }}</p>
        </div>
        <div class="book-item placeholder"></div>
      </div>
      <van-empty v-if="!bookListData || bookListData.length === 0" description="No data now" />
    </div>
    <PageFooter />
  </Main>
</template>

<script>
import Main from '@/views/layouts/index.vue'
import PageHeader from '@/components/page-header.vue'
import PageFooter from '@/components/page-footer.vue'
import { getBookList } from '@/api'
import BookCover from '@/components/book-cover.vue'

export default {
  name: 'ViewAll',

  components: {
    Main,
    BookCover,
    PageHeader,
    PageFooter
  },

  computed: {
    category() {
      return this.$route.query ? this.$route.query.category : ''
    }
  },

  created() {
    this.getBookList()
  },

  data() {
    return {
      bookListData: []
    }
  },

  methods: {
    onImgError(e) {
      e.target.className += ' error'
    },
    getBookList() {
      getBookList({
        page: 1,
        pagerow: 999,
        category: this.category
      }).then(res => {
        console.log(res)
        const { data } = res
        this.bookListData = data
      })
    },
    handleClickIcon() {
      this.$router.push({
        name: 'Favorite'
      })
    },
    gotoBookDetail(book) {
      this.$router.push({
        name: 'Book',
        query: {
          id: book.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.header-wrapper {
  .custom-iconfont {
    font-size: 24px;
  }
}

.booklist-data-wrapper {
  width: 100%;
  padding: 10px 12px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  box-sizing: border-box;

  .book-item {
    width: 32%;
    .book-name {
      width: 100%;
      padding-top: 6px;
      padding-bottom: 10px;
      word-break: normal;
      font-weight: bold;
      font-size: 14px;
    }

    .book-cover-wrapper {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      height: 140px;
      background-color: #eee;
      border-radius: 6px;
      overflow: hidden;
    }
  }
}
</style>
